package com.linx.common.web.interceptor;

import com.linx.common.core.exception.ServiceException;
import com.linx.common.core.utils.StringUtils;
import com.linx.common.mybatis.helper.ParkHelper;
import com.linx.common.satoken.utils.LoginHelper;
import com.linx.common.web.annotation.ParkCustomAnnotation;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
 * web的拦截是否选择园区
 *
 * @author Lion Li
 * @since 3.3.0
 */
@Slf4j
public class ParkWebInvokeInterceptor implements HandlerInterceptor {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("进入拦截器");

        if (handler instanceof HandlerMethod handlerMethod) {
            ParkCustomAnnotation annotation = handlerMethod.getMethodAnnotation(ParkCustomAnnotation.class);
            if (annotation != null) {
                // 这里可以进行自定义逻辑处理
                log.info("进入拦截器中："+ handlerMethod.getMethod().getName());
                if(!LoginHelper.isLogin()){
                    return true;
                }
                String s = ParkHelper.getparkInfoId();
                if(StringUtils.isBlank(s)){
                    throw new ServiceException("请选择园区后操作!");
                }
                // 可以根据需要返回 true 或 false
                return true;
            }
        }
        return true; // 继续执行下一个拦截器或目标方法
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
                                Exception ex) throws Exception {

    }


}
